
import { downFile,downFilePost } from '@/api/manage'
export const UtilsMixin = {
    data() {
        return {
        }
    },
    methods: {
        handleExportXls(fileName, url, params) {
            for (var p in params) {
                if (["pageNo", "total", "current", "pageSize", "sortField", "sortOrder", "showTotal", "pageSizeOptions"].indexOf(p) < 0 && params[p]) {
                    url += '&' + p + '=' + params[p]
                }
            }
            if (!fileName || typeof fileName != "string") {
                fileName = "导出文件"
            }
            this.$message.warning("正在导出,数据越多,需要时间越久。请耐心等待...")
            downFile(url).then((data) => {
                if (!data) {
                    this.$message.warning("文件下载失败")
                    return
                }
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
                } else {
                    let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + '.xls')
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link); //下载完成移除元素
                    window.URL.revokeObjectURL(url); //释放掉blob对象
                }
            })
        },
        handleExportXlsByPost(fileName, url, params) {
            for (var p in params) {
                if (["pageNo", "total", "current", "pageSize", "sortField", "sortOrder", "showTotal", "pageSizeOptions"].indexOf(p) < 0 && params[p]) {
                    url += '&' + p + '=' + params[p]
                }
            }
            if (!fileName || typeof fileName != "string") {
                fileName = "导出文件"
            }
            this.$message.warning("正在导出,数据越多,需要时间越久。请耐心等待...")
            downFilePost(url,params).then((data) => {
                if (!data) {
                    this.$message.warning("文件下载失败")
                    return
                }
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
                } else {
                    let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + '.xls')
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link); //下载完成移除元素
                    window.URL.revokeObjectURL(url); //释放掉blob对象
                }
            })
        },
        getRandomStr(len) {
            len = len || 32
            var $chars =
                'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
            var maxPos = $chars.length
            var pwd = ''
            for (let i = 0; i < len; i++) {
                pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
            }
            return pwd
        },
        currentHandlerRenderFunc(record) {
            let text = ''
            if (record.handlerUser) {
                if (record.handlerTime) {
                    // text = record.handlerUser + '/' + record.handlerTime
                    // var startDate = new Date(record.handlerTime).getTime()
                    // var endDate = new Date().getTime()
                    // var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000)
                    // text += '/' + parseInt(days) + '天'
                    text = record.handlerUser + '/' + record.handlerTime + '/' + record.stayDays + '天'
                }
            }
            return text
        },
        /**
         * 将驼峰转为下划线
         */
        camelToUnderline(camelStr) {
            if(camelStr){
                camelStr = camelStr.trim()
            }
            let str = camelStr.replace(/[A-Z]/g, function (s) {
                return '_' + s.toLowerCase();
            })
            return str
        },

        /**
         * 下划线转小驼峰
         */
        underlineToSmallCamel(str) {
            return str.toLowerCase().replace(/_([a-z])/g, function (s, s1) {
                return s1.toUpperCase();
            })
        }

    }
}